﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.BLL;
using CMN.Common;
using CMN.Entity;
using CMN.Models.Interfaces;
using System.Data;
namespace CMN.Models
{
    /// <summary>
    /// 功能模块Service
    /// </summary>
    public class Sys_ModuleService : ISearchDataService<Sys_ModuleEntity>
    {
        /// <summary>
        /// 新增功能模块
        /// </summary>
        /// <param name="Sys_ModuleEntity"></param>
        /// <returns>bool</returns>
        public string Insert(Sys_ModuleEntity sys_moduleEntity)
        {
            sys_moduleEntity.DataTable_Action = DataTableAction.Insert;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_ModuleInsertUpdateDelete(sys_moduleEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 返回功能模块查询列表
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="count">查询总个数</param>
        /// <returns>List</returns>
        public List<Sys_ModuleEntity> List(QueryParam qp, out int count)
        {
            BLLHelper bll = new BLLHelper();
            return bll.Sys_ModuleList(qp, out count);
        }

        /// <summary>
        /// 更新功能模块
        /// </summary>
        /// <param name="Sys_Module"></param>
        /// <returns>bool</returns>
        public string Update(Sys_ModuleEntity sys_moduleEntity)
        {
            sys_moduleEntity.DataTable_Action = DataTableAction.Update;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_ModuleInsertUpdateDelete(sys_moduleEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 删除功能模块
        /// </summary>
        /// <param name="Sys_Module"></param>
        /// <returns>bool</returns>
        public string Delete(Sys_ModuleEntity sys_moduleEntity)
        {
            sys_moduleEntity.DataTable_Action = DataTableAction.Delete;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_ModuleInsertUpdateDelete(sys_moduleEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 批量删除功能模块
        /// </summary>
        /// <param name="idList">删除id列表  2_8_9_45</param>
        /// <returns>bool</returns>
        public string DeleteList(string idList)
        {
            string[] list = Utils.SplitString(idList, "_");
            string strDel = Utils.ArrayToString(list, ",", false);
            QueryParam qp = new QueryParam();
            qp.PageSize = 1;
            qp.Where = string.Concat("M_ParentID  in (", strDel, ")");
            int recordNum = 0;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_ModuleList(qp, out recordNum).Count < 1)
            {
                if (bll.Sys_ModuleDelList(strDel) > 0)
                {
                    return "";
                }
                else
                {
                    return "失败";
                }
            }
            else
            {
           
                return "失败";
            
            }

        }

        /// <summary>
        /// 返回单个功能模块
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Sys_ModuleEntity</returns>
        public Sys_ModuleEntity Look(int id)
        {
            BLLHelper bll = new BLLHelper();
            return bll.Sys_ModuleDisp(id);
        }

        /// <summary>
        /// 模块编码是否已存在
        /// </summary>
        /// <param name="strCheck"></param>
        /// <returns></returns>
        public int IsHas(string strCheck)
        {
            QueryParam qp = new QueryParam();
            qp.Where = " M_PageCode='" + strCheck + "'";
            qp.PageIndex = 1;
            qp.PageSize = 1;
            int recordNum = 0;
            BLLHelper bll = new BLLHelper();
            if (bll.Sys_ModuleList(qp, out recordNum).Count > 0)
            {
                return recordNum;
            }
            else
                return recordNum;
        }
    }
}
